package com.mine.frame.mylandemo.ui

import android.content.Intent
import android.content.IntentFilter
import android.media.AudioManager
import android.net.ConnectivityManager
import android.os.Bundle
import android.util.Log
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.SnackbarUtils
import com.mine.frame.common.base.BaseActivity
import com.mine.frame.mylandemo.R
import com.mine.frame.mylandemo.adapter.IPAdapter
import com.mine.frame.mylandemo.mvp.iview.IMain
import com.mine.frame.mylandemo.mvp.presenter.MainPresenter
import com.mine.frame.mylandemo.receiver.NetworkReceiver
import com.mine.frame.mylandemo.ui.autorail.CurtainHoneycombAtv
import com.mine.frame.mylandemo.ui.autorail.DreamCurtainAtv
import com.mine.frame.mylandemo.ui.forick.Panel3In1Atv2
import com.mine.frame.mylandemo.ui.haobo.HaoBoInfraredAtv
import com.mine.frame.mylandemo.ui.jiecang.LiftCabinetAtv
import com.mine.frame.mylandemo.ui.jiecang.TranslationDeskAtv
import com.mine.frame.mylandemo.ui.mqtt.MQTTAtv
import com.mine.frame.mylandemo.ui.oupaike.BusDoorAtv
import com.mine.frame.mylandemo.ui.pl.PLMainAtv
import com.mine.frame.mylandemo.ui.study.DistanceAtv
import com.mine.frame.mylandemo.ui.study.MoveViewAtv
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.atv_move_view.*

class MainActivity : BaseActivity<IMain , MainPresenter>() ,IMain {

    private lateinit var ipAdapter : IPAdapter

    override fun initLayoutId(): Int = R.layout.activity_main

    override fun initPresenter(): MainPresenter = MainPresenter(this)

    override fun initLayoutAfter(savedInstanceState: Bundle?) {
        //网络监听
        NetworkUtils.registerNetworkStatusChangedListener(object :
            NetworkUtils.OnNetworkStatusChangedListener {
            override fun onDisconnected() {
                Log.e("NetworkReceiver:" ," Network onDisconnected")
            }

            override fun onConnected(networkType: NetworkUtils.NetworkType?) {
                Log.e("NetworkReceiver:" ," Network onConnected : $networkType")
            }
        })
        onRegisterNetWorkReceiver()
        ClickUtils.applySingleDebouncing(arrayOf(btnCheckWifi ,btnOpenWifi ,btnGetNativeIP ,btnWifiList ,
            btnGetIPList ,btnMac ,btnOrgan ,btnRainbow ,btnDesk,btnDesk2 ,btnBusDoor ,btnLift ,btnTest ,btnPl ,btnTestMove ,
            btnMQTT ,btnHaoBo , btnDistance)){
            it?.let { view ->
                when(view.id){
                    R.id.btnPl ->{
                        ActivityUtils.startActivity(PLMainAtv::class.java)
                    }
                    R.id.btnCheckWifi ->{
                        checkWifi()
                    }
                    R.id.btnOpenWifi ->{
                        NetworkUtils.openWirelessSettings()
                    }
                    R.id.btnGetNativeIP->{
                        mPresenter.getNativeIP()
                    }
                    R.id.btnWifiList->{
                        mPresenter.getWifiList()
                    }
                    R.id.btnGetIPList ->{
                        mPresenter.getIPList()
                    }
                    R.id.btnTest ->{
                        val intent = Intent(this@MainActivity , TestConnectAtv::class.java)
                        intent.putExtra("sel-ip" ,"192.168.0.105")
                        ActivityUtils.startActivity(intent)
                    }
                    R.id.btnMac ->{
                        val intent = Intent(this@MainActivity , Panel3In1Atv2::class.java)
                        intent.putExtra("sel-ip" ,"192.168.0.177")
                        ActivityUtils.startActivity(intent)
                    }
                    R.id.btnOrgan ->{
                        val intent = Intent(this@MainActivity , CurtainHoneycombAtv::class.java)
                        intent.putExtra("sel-ip" ,"192.168.0.177")
                        ActivityUtils.startActivity(intent)
                    }
                    R.id.btnRainbow ->{
                        val intent = Intent(this@MainActivity , DreamCurtainAtv::class.java)
                        intent.putExtra("sel-ip" ,"192.168.0.177")
                        ActivityUtils.startActivity(intent)
                    }
                    R.id.btnDesk ->{
                        val intent = Intent(this@MainActivity , TranslationDeskAtv::class.java)
                        //intent.putExtra("sel-ip" ,"192.168.0.177")
                        intent.putExtra("sel-ip" ,"192.168.0.177")
                        intent.putExtra("oldCommand" ,true)
                        ActivityUtils.startActivity(intent)
                    }
                    R.id.btnDesk2 ->{
                        val intent = Intent(this@MainActivity , TranslationDeskAtv::class.java)
                        //intent.putExtra("sel-ip" ,"192.168.0.177")
                        intent.putExtra("sel-ip" ,"192.168.0.177")
                        intent.putExtra("oldCommand" ,false)
                        ActivityUtils.startActivity(intent)
                    }

                    R.id.btnBusDoor ->{
                        val intent = Intent(this@MainActivity , BusDoorAtv::class.java)
                        intent.putExtra("sel-ip" ,"192.168.0.177")
                        ActivityUtils.startActivity(intent)
                    }

                    R.id.btnLift ->{
                        val intent = Intent(this@MainActivity , LiftCabinetAtv::class.java)
                        intent.putExtra("sel-ip" ,"192.168.0.178")
                        ActivityUtils.startActivity(intent)
                    }

                    R.id.btnTestMove ->{
                        ActivityUtils.startActivity(MoveViewAtv::class.java)
                    }

                    R.id.btnMQTT->{
                        ActivityUtils.startActivity(MQTTAtv::class.java)
                    }

                    R.id.btnHaoBo->{
                        ActivityUtils.startActivity(HaoBoInfraredAtv::class.java)
                    }
                    R.id.btnDistance ->{
                        ActivityUtils.startActivity(DistanceAtv::class.java)
                    }
                }
            }
        }
    }

    /**
     * 检查wifi是否可用
     */
    private fun checkWifi(){
        if (!NetworkUtils.getWifiEnabled() || !NetworkUtils.isWifiConnected()){
            SnackbarUtils.with(btnCheckWifi).setMessage("WIFI不可用，请检查").showWarning()
        }else{
            SnackbarUtils.with(btnCheckWifi).setMessage("WIFI可用").showSuccess()
        }
    }




    override fun scanIP(ip: MutableList<String>) {
        if (!this::ipAdapter.isInitialized){
            ipAdapter = IPAdapter()
            mRecycler.adapter = ipAdapter
            ipAdapter.addChileClickListener { _, position ->
                val ipStr = ipAdapter.getIPFromPosition(position)
                val intent = Intent(this@MainActivity ,ConnectAtv::class.java)
                intent.putExtra("sel-ip" ,ipStr)
                ActivityUtils.startActivity(intent)
            }
        }
        ipAdapter.refreshIP(ip)
    }

    override fun macBack(mac: MutableMap<String, String>) {
        if (this::ipAdapter.isInitialized){
            ipAdapter.refreshMac(mac)
        }
    }

    private val mReceiver by lazy { NetworkReceiver() }

    private fun onRegisterNetWorkReceiver(){
        val filter = IntentFilter()
        filter.addAction("android.media.VOLUME_CHANGED_ACTION")
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
        registerReceiver(mReceiver,filter)
        Log.e("NetworkReceiver:" ,"注册广播")
    }

    private fun onUnregisterNetWorkReceiver(){
        unregisterReceiver(mReceiver)
        Log.e("NetworkReceiver:" ,"注销广播")
    }


    override fun onDestroy() {
        super.onDestroy()
        onUnregisterNetWorkReceiver()
    }
}